package com.apes.scm.masterdata.frame.repository;

import com.apes.framework.jpa.repository.CustomRepository;
import com.apes.scm.masterdata.frame.model.Branch;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Date;
import java.util.List;

public interface BranchRepository extends CustomRepository<Branch, String> {

    @Query("select T from Branch T where 1 = 1 and (id like %:value%  or name like %:value%) ")
    Page<Branch> find(@Param("value") String value, Pageable pageable);

    List<Branch> findAllByValidEqualsOrderByParentsDesc(boolean vaild);

    @Query(value = "SELECT t.* FROM mas_branch t WHERE t.id IN(" +
            "SELECT DISTINCT t.branch_id FROM mas_dept t WHERE t.company_id IN (?1)) and (id like %?2%  or name like %?2%)",nativeQuery = true)
    List<Branch> findByLocalPermit(List<String> orgIds, String value);

    //根据外部ID查找部门
    Branch findByOrigin(String Origin);

    @Query("SELECT T FROM Branch T where T.lastUpdatedStamp > :times " +
            "OR (T.id > :id AND T.lastUpdatedStamp = :times) ORDER BY T.lastUpdatedStamp, T.id ASC")
    List<Branch> findAllBranchByTs(@Param("times") Date times, @Param("id") String id, Pageable pageable);

}
